<template>
  <div class="submit">
    <el-button type="primary" @click="submit">提交</el-button>
    <el-button @click="$router.push('/question')">取消</el-button>
  </div>
</template>
<script>
import { request } from "@/api/request";
import { shake } from "@/utils/shake"; // 引入防抖
export default {
  props: {
    data: { type: Object, default: null },
    catalougeid: { type: Number, default: -1 },
    chapterid: { type: Number, default: -1 },
  },
  data() {
    return {
      subm: shake(this.sub, 1000),
      routerid: this.$route.query.id || -1,
    };
  },
  methods: {
    // 点击提交
    submit() {
      this.subm();
      // console.log(this.data);
    },
    sub() {
      let params = {
        url: "question",
        params: this.data,
      };
      let msg = "";
      if (this.routerid !== -1) {
        params.route = "edit";
        params.method = "PUT";
        params.query = this.routerid;
        msg = "编辑";
      } else {
        params.route = "add";
        params.method = "POST";
        msg = "添加";
      }
      request({ ...params }).then((res) => {
        if (res.code !== "200") return this.$message.info(res.msg);
        this.$message.success(`${msg}试题成功`);
        this.$router.push("/question");
      });
    },
  },
};
</script>
